package com.sys.service;


import com.sys.GlobalVariable;
import com.sys.entity.Card;
import com.sys.exception.DuplicateCardException;
import com.sys.utils.DbUtil4;
import com.sys.utils.DbUtil4Factory;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CardService {
    public void add(String name, String qq, String phone, Integer userId) throws Exception {
        DbUtil4 db = null;
        String selectSQL = "select count(1) as num from t_card where cname = ? and cqq=?  and cphone=? and uid =? ";
        String sql = "INSERT INTO t_card (cname,cqq,cphone,uid) VALUES (?,?,?,?)";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            List<Map<String, Object>> list = db.query(selectSQL, new Object[]{name, qq, phone,userId});
            // 由于查询的是 select count(1) ，所以一定有一个值
//            Map<String, Object> map = list.get(0);
//            Long num = (Long) map.get("num");
            // 将上面两行代码合成一行
            Long num = (Long)  list.get(0).get("num");
            if(num == 0){ // 没有重复的记录
                db.update(sql, new Object[]{name, qq, phone,userId});
            }else{ // 如果名片存在，抛出异常
                throw new DuplicateCardException("名片已经存在！");
            }

        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }


    public int update(String newName, String newQq, String newPhone, int cid) throws Exception {
        DbUtil4 db = null;
        String sql = "update t_card set cname = ?, cqq = ?, cphone = ? where cid = ?";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            int i = db.update(sql, new Object[]{newName, newQq, newPhone,cid});
            return i;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }
    public int delete(int cid) throws Exception {
        DbUtil4 db = null;
        String sql = "UPDATE t_card SET del = 1 WHERE cid = ?";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            int j = db.update(sql, new Object[]{cid});
            return j;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }
    public List<Card> showList(int uid, String username, String qq, String phone) throws Exception {
        DbUtil4 db = null;
        List<Card> list = null;
        String sql = "SELECT cid,cname,cqq,cphone,create_time,modify_time,del FROM t_card where del = 0 and uid = ?";

        // 查询条件不确定，需要动态拼装sql
        // 动态添加参数，不能使用数组，因为数组定义好后，是固定长度
        List paramList = new ArrayList();
        paramList.add(uid);

        if(username!=null && !"".equals(username)){
            sql += " and cname like ?";
            paramList.add(username+"%");
        }
        if(qq!=null && !"".equals(qq)){
            sql += " and cqq like ?";
            paramList.add(qq+"%");
        }
        if(phone!=null && !"".equals(phone)){
            sql += " and cphone like ?";
            paramList.add(phone+"%");
        }
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            list = db.query(sql, paramList.toArray() , Card.class);
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
       return list;
    }


    public Card showById(String cid) throws Exception{
        List<Card> list = null;
        DbUtil4 db = null;
        boolean t = false;
        String sql = "SELECT cid,cname,cqq,cphone,create_time,modify_time,del FROM t_card WHERE cid = ? and del = 0";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            list = db.query(sql, new Object[]{ cid }, Card.class);
            if(list!=null && !list.isEmpty()){
                Card card = list.get(0);
                return card;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
        return null;
    }
}
